package com.yupi.springbootinit.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.mapper.SysteminfoMapper;
import com.yupi.springbootinit.mapper.VipuserlistMapper;
import com.yupi.springbootinit.model.entity.Systeminfo;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.entity.Vipuserlist;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.service.UserService;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.constructor.Construct;
import org.yaml.snakeyaml.constructor.Constructor;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class SignResetTask {
    @Resource
    private UserService userService; // 假设你有一个用户服务来处理用户数据

    // 每天零点执行此任务
    @Scheduled(cron = "0 0 0 * * ?")
    public void resetAllUserSigns() {
        // 调用用户服务来更新所有用户的isSign字段
        userService.resetAllUserSignsToZero();
    }

    @Resource
    private PostService postService; // 假设你有一个帖子服务来处理帖子数据

    // 三个月执行一次此任务
    @Scheduled(cron = "0 0 0 1 1/3 ?")
    public void deleteOldPosts() {
        // 调用帖子服务来删除所有已过期的帖子
        postService.deleteOldPost();
    }

    @Resource
    private SysteminfoMapper systeminfoMapper; // 假设你有一个系统信息映射器来处理系统数据


    // 每小时执行一次此任务，用于更新系统状态或进行其他维护操作
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateUserNum() {

        Systeminfo systeminfo = new Systeminfo();
        // 调用系统信息映射器来更新系统状态或其他相关操作
        long totalUserNum = userService.count(); // 获取用户总数
        long totalPostNum = postService.count(); // 获取帖子总数
        systeminfo.setEnrollment((int) totalUserNum);
        systeminfo.setTotalpostNum((int) totalPostNum);
        QueryWrapper<Systeminfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createTime"); // 根据创建时间降序排序
        queryWrapper.last("LIMIT 1"); // 限制结果数量为 1
        Systeminfo latestRecord = systeminfoMapper.selectOne(queryWrapper);
        if (latestRecord == null) {
            systeminfo.setSendSmsNum(0);
        }else {
            systeminfo.setSendSmsNum(latestRecord.getSendSmsNum());
        }
        systeminfoMapper.insert(systeminfo);
    }


    @Resource
    private VipuserlistMapper vipuserlistMapper; // 假设你有一个VIP用户列表映射器来处理VIP用户数据


    /**
     * 根据vip创建时间，到一个月去除身份
     */
    // 每分钟执行一次此任务，用于更新VIP用户状态或进行其他维护操作
    @Scheduled(cron = "0 0 0 1 1/1 ?")
    public void delUserVipStatus() {
        LocalDateTime oneMonthAgo = LocalDateTime.now().minusMonths(1);
        // 调用VIP用户列表映射器来更新VIP用户状态或其他相关操作
        QueryWrapper<Vipuserlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("createTime",oneMonthAgo); // 根据创建时间降序排序
        List<Vipuserlist> vipuserlists = vipuserlistMapper.selectList(queryWrapper);
        for (Vipuserlist vipuserlist : vipuserlists) {
            Long userId = vipuserlist.getUserId();
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id",userId);
            User user = userService.getOne(userQueryWrapper);
            if (user != null) {
                user.setUserRole(UserConstant.DEFAULT_ROLE);
                userService.updateById(user);
                vipuserlistMapper.deleteById(vipuserlist);
            }
        }
    }
}
